local skel = fk.CreateSkill{
  name = "lb__lunyue",
  tags = {Skill.Compulsory},
  max_branches_use_time = {
    ["putcard"] = {
      [Player.HistoryRound] = 3
    },
    ["skip"] = {
      [Player.HistoryRound] = 1
    },
  }
}

Fk:loadTranslationTable{
  ["lb__lunyue"] = "论阅",
  --[":lb__lunyue"] = "<b>锁定技</b>，每轮限一次，你跳过出牌阶段；每回合结束时，若当前回合角色于回合内弃置过牌，你可将弃牌堆中的这些牌置于牌堆底，然后你摸一张牌并获得一个出牌阶段。",
  -- 8.30
  [":lb__lunyue"] = "<b>锁定技</b>，每轮限一次，你跳过出牌阶段；每轮限三次，每回合结束时，若当前回合角色于回合内弃置过牌，你可将弃牌堆中的这些牌置于牌堆底，然后你摸一张牌并获得一个出牌阶段。",

  ["#lb__lunyue_skip"] = "论阅",

  ["$lb__lunyue1"] = "需要一些严厉的教导吗？",
  ["$lb__lunyue2"] = "由我提问了，期待各位的应答。",
}

skel:addEffect(fk.TurnEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and #player:getTableMark("lb__lunyue-turn") > 0
    and player:usedSkillTimes(skel.name, Player.HistoryRound, "putcard") < 3 --8.30
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player,{skill_name = skel.name})
  end,
  on_use = function (self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "putcard")
    player.room:moveCards({
      ids = player:getTableMark("lb__lunyue-turn"),
      toArea = Card.DrawPile,
      moveReason = fk.ReasonPut,
      drawPilePosition = -1,
      skillName = skel.name,
      proposer = player,
      moveVisible = true,
    })
    player:drawCards(1,skel.name)
    player:gainAnExtraPhase(Player.Play,skel.name,false)
  end,
}, { check_skill_limit = true})

skel:addEffect(fk.EventPhaseChanging, {
  is_delay_effect = true,
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and data.phase == Player.Play
    and player:usedSkillTimes(skel.name, Player.HistoryRound, "putcard") == 0
  end,
  on_use = function (self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "skip")
    data.skipped = true
  end,
}, { check_skill_limit = true})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name, true) then
      for _, move in ipairs(data) do
        local current = Fk:currentRoom():getCurrent()
        if not current then return end
        local id = current.id
        if move.toArea == Card.DiscardPile and move.from and move.proposer and move.from.id == id and move.proposer.id == id then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(player.room.discard_pile, info.cardId) then
              return true
            end
          end
        end
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.DiscardPile and not table.contains(player.room.discard_pile, info.cardId) then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local ids = player:getTableMark("lb__lunyue-turn")
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        local id = Fk:currentRoom():getCurrent().id
        for _, info in ipairs(move.moveInfo) do
          if table.contains(room.discard_pile, info.cardId) and move.from and move.proposer and move.from.id == id and move.proposer.id == id then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.DiscardPile and not table.contains(room.discard_pile, info.cardId) then
          table.removeOne(ids, info.cardId)
        end
      end
    end
    room:setPlayerMark(player, "lb__lunyue-turn", ids)
  end,
})

return skel